<?php
// Chat side for studenkel med jquery.
include("banner.html");
include("dogs.php");
// Sjekk om bruker er logget inn
if(!isset($_SESSION['user'])){
die("Du er ikke logget inn. <a href=../index.php>Login</a>");
}

//Avslutte chat
if(isset($_GET['logout'])){

//exit message
$fp = fopen("log.html", 'a');
fwrite($fp, "<div class='msgln'><i>User ". $_SESSION['user'] . " Har forlatt chatten</i><br></div>");
fclose($fp);

header("Location: index.php"); //redirect tilbake til studenkel
}


// hent bruker lista
echo "<div id=\"brukere\"><pre>";
getUsers();
echo "</pre></div>";

?>
<div id="profileinfo">
<div id="chat">  
    <div id="menu" style="padding:12.5px 25px 12.5px 25px;">  
        <p class="welcome" style="float:left;">Velkommen, <b><?php echo $_SESSION['user']; ?></b></p>  
        <p class="logout" style="float:right;"><a id="exit" href="#">Exit Chat</a></p>  
        <div style="clear:both"></div>  
    </div>  
    

    <!-- chatbox -->
    <div id="chatbox">
	<?php
	// Hent ut chat loggen
	if(file_exists("log.html") && filesize("log.html") > 0){
		$handle = fopen("log.html", "r");
		$contents = fread($handle, filesize("log.html"));
		fclose($handle);
		echo $contents;
	}
	// Scroll til bunnen
	?>	
	<script type="text/javascript">
	var bunn = $("#chatbox").prop("scrollHeight") -20;
	$("#chatbox").animate({ scrollTop: bunn }, 'normal'); //skroll til bunnen
	</script>
	
	</div>  
      
    <form name="message" action="">  
        <input name="usermsg" type="text" id="usermsg" size="63" />  
        <input name="submitmsg" type="submit"  id="submitmsg" value="Send" />  
    </form>  
</div>  
</div>
<!-- <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> -->  
<script type="text/javascript">  


// jQuery Document  
$(document).ready(function(){  
  
  //hvis brukeren vil avslutte chat session
  $("#exit").click(function(){
  var exit = confirm("Er du sikker på at du vil avslutte chat?");
  if(exit==true){ window.location = 'chat.php?logout=true'; }
  });

  //Hvis brukeren sender en chat melding
  $("#submitmsg").click(function(){
  var clientmsg = $("#usermsg").val(); //hente value fra usermsg
  $.post("post.php", {text: clientmsg}); //sende post request
  $("#usermsg").val(""); //resete value - fixed
  return false;
  });
  
 // Ajax funksjon for å laste loggen
function loadLog(){
    var oldscrollHeight = $("#chatbox").prop("scrollHeight") - 20;
	$.ajax({
	url: "log.html",
	cache: false,
	success: function(html){
	$("#chatbox").html(html); //setter chat loggen inn i #chatbox div.
	
	// auto scroll
	
	var newscrollHeight = $("#chatbox").prop("scrollHeight") -20;
	//console.log("old: "+oldscrollHeight+" new: "+newscrollHeight);
	if(newscrollHeight > oldscrollHeight){
	$("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //skroll til bunnen
	}

	},

});
} 
  
  //reloader log.html hvert 2,5 sekund.
 setInterval(loadLog, 2500);
 
  });  
</script>




	
  
</body>
	<?php
	include ('info.html');
	?>
</html>  

